import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'
function useInterval(fn,delay){
    const callbackFn=useRef(fn)
    useLayoutEffect(()=>{
        callbackFn.current=fn
    })
    useEffect(()=>{
        const timer=setInterval(()=>{
            callbackFn.current()
        },delay)
        return ()=>{
            clearInterval(timer)
        }
    },[])
}
export default function Trap() {
    const [count,setCount] = useState(0)
    const updataCount=()=>{
        setCount(count+1)
    }
    // useInterval(updataCount,1000)
    const ref=useRef(updataCount)
    ref.current=updataCount
    useEffect(()=>{
        setInterval(()=>{
            ref.current()
        },1000)
        return ()=>{
            clearInterval()
        }
    },[])
  return (
    <div>{count}</div>
  )
}
